using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Sauce.Internal.Extension;

namespace Voodoo.Sauce.Debugger
{
	public class EventConsoleInformationScreen : Screen
	{
		private const string DEFAULT_HEADER_FIELDS = "other header fields";

		private const string EVENT_DATE_FORMAT = "dd/MM/yyyy HH:mm:ss";

		[Header("Console Information")]
		[SerializeField]
		private EventConsoleErrorScreen errorScreen;

		[SerializeField]
		private EventInformationList eventInformationListPrefab;

		[SerializeField]
		private EventInformationLineItem eventInformationLineItemPrefab;

		[SerializeField]
		private Text eventName;

		[SerializeField]
		private Text eventDate;

		[SerializeField]
		private GameObject eventSendingStatus;

		[SerializeField]
		private GameObject eventSentStatus;

		[SerializeField]
		private GameObject eventErrorStatus;

		[SerializeField]
		private Transform eventInformationContainer;

		[SerializeField]
		private Button eventCopyJson;

		[SerializeField]
		private Button eventSeeError;

		private readonly Dictionary<string, EventInformationList> _eventInformationListDictionary = new Dictionary<string, EventInformationList>();

		private readonly Stack<EventInformationLineItem> _eventInformationLinePool = new Stack<EventInformationLineItem>();

		private void OnEnable()
		{
			RefreshInformationSize();
		}

		private EventInformationList CreateEventInformationList(string title)
		{
			EventInformationList eventInformationList = Object.Instantiate(eventInformationListPrefab, eventInformationContainer);
			_eventInformationListDictionary.Add(title, eventInformationList);
			eventInformationList.Initialize(title);
			return eventInformationList;
		}

		private void CreateEventInformationLine(KeyValuePair<string, object> information, Transform parent)
		{
			EventInformationLineItem eventInformationLineItem;
			if (_eventInformationLinePool.Count == 0)
			{
				eventInformationLineItem = Object.Instantiate(eventInformationLineItemPrefab, parent);
			}
			else
			{
				eventInformationLineItem = _eventInformationLinePool.Pop();
				eventInformationLineItem.transform.SetParent(parent);
				eventInformationLineItem.gameObject.SetActive(value: true);
			}
			eventInformationLineItem.UpdateData(information);
		}

		private void CleanEventInformationBody()
		{
			foreach (KeyValuePair<string, EventInformationList> item in _eventInformationListDictionary)
			{
				foreach (Transform item2 in item.Value.GetContainer)
				{
					item2.gameObject.SetActive(value: false);
					_eventInformationLinePool.Push(item2.GetComponent<EventInformationLineItem>());
				}
				item.Value.gameObject.SetActive(value: false);
			}
		}

		private void RefreshInformationSize()
		{
			foreach (KeyValuePair<string, EventInformationList> item in _eventInformationListDictionary.Where((KeyValuePair<string, EventInformationList> keyValue) => keyValue.Value.gameObject.activeSelf))
			{
				item.Value.GetContainer.RefreshHierarchySize();
			}
		}

	}
}
